<?php

namespace App\Datas;

use App\Tools\FunTool;
use Illuminate\Support\Facades\DB;

class ModelList extends Base
{
    const STATUS_ANLE = 0;

    const IS_VIP = 1;

    const IS_HOT = 1;

    const IMG2IMG = 1;

    public $tableName = "model_list";

	public $sVersion='1.0.2';
	public $sPlatform='1';//1:ios 2:安卓

    private static $instance = null;

    private function __construct()
    {
		$this->sVersion = $_SERVER['HTTP_AIVERSION'] ?? '1.0.2';
		$this->sPlatform = $_SERVER['HTTP_PLATFORM'] ?? 1;

		//if(1 == $this->sPlatform && '1.8.0' == $this->sVersion) {
		//	$this->tableName = 'model_list_special';
		//}
		// if(2 == $this->sPlatform) {
		 	$this->tableName = 'model_list_special';
		// }
    }

    public static function getInstance() {
        if (empty(self::$instance) && !(self::$instance instanceof ModelList)) {
            self::$instance = new self();
        }
        return self::$instance;
    }


    /**
     * 格式化列表
     * @param $list
     * @return mixed
     */
    public function formatList($list) {
        foreach ($list as &$item) {
            $item = (array)$item;
            $this->formatItem($item);
        }
        return $list;
    }

    /**
     * 格式化item
     * @param $item
     * @return void
     */
	public function formatItem(&$item) {
		if(array_key_exists("img", $item)) {
			$item["img"] = env("APP_FILES_URL")."/".$item["img"];
			$item["height"] = 600;
			$item["width"] = 512;
		}
		if(array_key_exists("video", $item)) {
			$item["video"] = env("APP_FILES_URL")."/".$item["video"];
			$item["video_img"] = env("APP_FILES_URL")."/".$item["video_img"];
			$item["video_webp"] = env("APP_FILES_URL")."/".$item["video_webp"];
			isset($item["detail_video_img"]) && $item["detail_video_img"] = env("APP_FILES_URL")."/".$item["detail_video_img"];
			$item["height_v"] = (int)$item["height_v"];
			$item["width_v"] = (int)$item["width_v"];
		}
		if(array_key_exists("hot", $item)) {
			$item["hot"] = $item["hot"] == self::IS_HOT ? FunTool::getHotImg() : "";
		}
		if(array_key_exists("vip", $item)) {
			$item["vip"] = $item["vip"] == self::IS_VIP ? FunTool::getVipImg() : "";
		}
	}

    /**
     * 首页推荐的列表
     * @return mixed
     */
    public function getIndexList() {
        $list =  DB::table($this->tableName)
            ->select(["id",'name','hot'])
            ->where(["show_index" => 1,'status' => self::STATUS_ANLE])
            ->orderByDesc("sort")
            ->orderByDesc("id")
            ->get()->toArray();
        return $this->formatList($list);
    }
    /**
     * 首页推荐的列表--new
     * @return mixed
     */
    public function getNewIndexList() {
        $list =  DB::table($this->tableName)
            ->select(["id",'name','hot','is_life_vip_pro'])
            ->where(["show_index" => 1,'status' => self::STATUS_ANLE])
            ->orderByDesc("sort")
            ->orderByDesc("id")
            ->get()->toArray();
        return $this->formatList($list);
    }

    /**
     * 模型列表页
     * @return mixed
     */
    public function getAllList() {
        $list =  DB::table($this->tableName)
            ->select(["id",'img','name','hot','vip'])
            ->where(['status' => self::STATUS_ANLE])
            ->orderByDesc("sort")
            ->orderByDesc("id")
            ->get()->toArray();
        return $this->formatList($list);
    }
    /**
     * 模型列表页--新
     * @return mixed
     */
    public function getNewAllList() {
        $list =  DB::table($this->tableName)
            ->select(["id",'img','name','hot','vip',"is_life_vip_pro"])
            ->where(['status' => self::STATUS_ANLE])
            ->orderByDesc("sort")
            ->orderByDesc("id")
            ->get()->toArray();
        return $this->formatList($list);
    }

    /**
     * 图生图列表
     * @return mixed
     */
    public function getImg2ImgList() {
        $list =  DB::table($this->tableName)
            ->select(["id","video","video_img","video_webp","detail_video_img",'name','hot',"vip",'height_v','width_v',"dot"])
            ->where(['status' => self::STATUS_ANLE,"img2img" => self::IMG2IMG])
            ->orderByDesc("sort")
            ->orderByDesc("id")
            ->get()->toArray();
        return $this->formatList($list);
    }

    public function getAbleCount() {
        return DB::table($this->tableName)->where(['status' => self::STATUS_ANLE])->count();
    }

    public function getModelVipById($id) {
        $res = $this->getOneInfoById($id,["vip"]);
        return (!empty($res) && $res["vip"] == self::IS_VIP) ? true : false;
    }

    public function getModelInfoById($id) {
        return $this->getOneInfoById($id);
    }

	/**
	 * 更新
	 * @param array $aWhere
	 * @param array $aUpdate
	 * @return int
	 */
	public function editModelByWhere($aWhere, $aUpdate) {
		return DB::table($this->tableName)
				 ->where($aWhere)
				 ->update($aUpdate);
	}
}